begintownscript;

variables;
short bmessage,gone,combat,rctx,join_pc;
string custom_m,custom_m2,custom_m3;

body;

beginstate INIT_STATE;

	if(get_flag(99,4) == 1){
		split_off_one_char(32,43,0,0);
		add_char_to_party(10);
		march_party(32,42);
		force_view_center(32,42);
		set_character_facing(4,0);
		force_instant_terrain_redraw();
		block_entry(1);
	}
	else{
		erase_char(10);
	}

	set_crime_tolerance(1);

	if(get_flag(26,0) < 6){
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0,"Dervish Malachai pulls you off to the side of the dirt path leading into the wooden hut.  _Psst!  Come to the side._",0);
		add_dialog_str(1,"_We cannot speak loudly.  From now on, we whisper.  This is their war party.  If we rile them up, we lose the advantage of surprise.  Anthony has been here multiple times; he's good at sneaking around and losing people.  We'll take his route._",0);
		add_dialog_str(2,"_First, let's take a look through that gap in the foliage.  They shouldn't be well-guarded, but let's look nevertheless._",0);
		bmessage = run_dialog(1);
		set_flag(26,0,6);
	}

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;

	if(get_crime_level() > 0){
		message_dialog("Attacking the dervish- the PERFECT way to let the enemy know you're there, AND to prevent yourself from fighting back effectively!","You are subdued and slain on the spot.  It is painful.");
		kill_char(1000,3,0);
		end_scenario(0);
		end();
	}

	if(get_flag(26,0) >= 9 && get_flag(26,0) <= 14){
		combat = get_ran(1,0,2);
		while((combat - 1) > 0){
			if(get_ran(1,0,2) == 2){
				play_sound(70);
				if(get_ran(1,0,1) == 0){
					play_sound(70);
				}
			}
			else{
				if(get_ran(1,0,1) == 1){
					play_sound(70);
				}
				else{
					play_sound(2);
				}
			}
			combat = (combat - 1);
		}
	}

break;

beginstate 10;

	if(get_flag(26,0) < 15){
		message_dialog("You can't leave yet!  You have a mission to complete.","");
		block_entry(1);
	}

break;

beginstate 11;

	if(get_flag(26,0) < 7){
		message_dialog("_Psst!  Before we move onward, we should assess their guards!  Let's stay behind and take a peek first._","");
		block_entry(1);
	}

break;

beginstate 12;

	if(get_flag(26,0) >= 7 && get_flag(26,0) < 9){
		block_entry(1);
	}

	if(get_flag(26,0) < 7){
		message_dialog("You look through the thicket to see how this place is guarded...","");

		set_total_visibility(1);
		force_view_center(30,30);
		force_instant_terrain_redraw();
		pause(10);

		message_dialog("_Only one?  Hmm...  It'd still make plenty of noise upon death.  I won't bother trying to sneak up and kill it, since it seems pretty attentive, and would shout an alarm rather quickly._","_Let's continue heading up this path._");
		set_total_visibility(0);
		force_view_center(char_loc_x(0),char_loc_y(0));
		force_instant_terrain_redraw();
		block_entry(1);
		set_flag(26,0,7);

	}
	if(get_flag(26,0) == 8){
		play_sound(18);
		play_sound(48);
		play_sound(23);
		erase_char(6);
		erase_char(7);
		message_dialog("Well, THAT certainly isn't very quiet.  Dervish Malachai is doing his thing, and now it's time for you to do yours.","");

		set_flag(26,0,9);
	}

break;

beginstate 13;

	if(get_flag(26,0) < 8){
		message_dialog("_Psst!  Not this way.  Keep on following Anthony's path around this place and we'll be fine._","");
		block_entry(1);
	}

	if(get_flag(26,0) == 8){
		play_sound(18);
		play_sound(48);
		play_sound(23);
		erase_char(6);
		erase_char(7);
		message_dialog("Well, THAT certainly isn't very quiet.  Dervish Malachai is doing his thing, and now it's time for you to do yours.","");

		set_flag(26,0,9);
	}

break;

beginstate 14;

	message_dialog("This way leads north, and into firmly nephil-held lands.  Even if you were interested in killing yourself, there would be far less painful ways of going about it than charging into the enemy's territory.","");
	block_entry(1);

break;

beginstate 15;

	if(get_flag(26,0) == 8){
		message_dialog("_Stay away!  Go to the other entrance.  Quickly, and please!_","");
		block_entry(1);
	}
	if(get_flag(26,0) > 8 && get_flag(26,0) < 15){
		message_dialog("You don't have time to be wasting here!","");
		block_entry(1);
	}

	if(get_flag(26,0) < 8){
		clear_buffer();
		append_string("_Okay, ");
		append_char_name(0);
		append_string(".  I want you to listen to me closely, since these are my directions for you._");
		get_buffer_text(custom_m);
		reset_dialog();
		add_dialog_str(0,"This is the base, alright.  The nephils here seem to be carousing, rabble-rousing, and otherwise keeping spirits afloat, much to their imminent misfortune.",0);
		add_dialog_str(1,custom_m,0);
		add_dialog_str(2,"_I will wait here for about a minute.  I want you to round the corner and go to the other entrance to the building.  Again, I'll try to keep the enemies in this room only._",0);
		add_dialog_str(3,"_What I want out of you is that I want you to search every individual room in the base.  Take care of any nephils if you see them.  Try not to dawdle, as I may need your help, depending._",0);
		add_dialog_str(4,"_If all works out, we should have everyone in this base with their makers within the next ten minutes._",0);
		if(get_flag(99,0) == 1)
			add_dialog_choice(0,"Will you be safe?");
		if(get_flag(99,0) == 2)
			add_dialog_choice(0,"Try hard.  I want this to be painless.");
		bmessage = run_dialog(1);

		reset_dialog();
		if(get_flag(99,0) == 1){
			clear_buffer();
			append_string("_");
			append_char_name(0);
			append_string("...  I'll be fine, I promise.  Don't concern yourself over me.  I'm more concerned about you, although I probably shouldn't._");
			get_buffer_text(custom_m);
			add_dialog_str(0,custom_m,0);
		}
		if(get_flag(99,0) == 2){
			add_dialog_str(0,"He nods.  _So be it, then.  I'll wait here.  Don't worry about waiting for me to start, you'll notice that instantaneously._",0);
		}
		add_dialog_str(1,"_Go through the thicket to the south.  Things will start properly soon thereafter._",0);
		bmessage = run_dialog(1);

		set_flag(26,0,8);
		gone = remove_char_from_party(404);
		gone = remove_char_from_party(113);
		activate_hidden_group(1);
		block_entry(1);
	}

break;

beginstate 16;

	if(get_flag(26,0) < 10){
		reset_dialog();
		add_dialog_str(0,"You step into the small longhouse-type hut.  The roof is made out of skins tied together, and it has apparently been working pretty well.",0);
		add_dialog_str(1,"The hallways seem quiet enough.  Malachai's activities must have drawn everyone to the feasting hall, leaving this place clear.",0);
		add_dialog_str(2,"Meanwhile, there are various side passages that sound empty.  They might as well be empty, but you still have to check.  Once you are sure that all of the halls are empty, you might be able to help Malachai.",0);
		bmessage = run_dialog(1);
		set_flag(26,0,10);
	}

break;

beginstate 17;

	if(get_flag(27,0) == 0){
		if(get_flag(32,0) == 0)
			message_dialog("Nobody here.","");
		if(get_flag(32,0) == 1)
			message_dialog("Nobody here either.","");
		if(get_flag(32,0) == 2)
			message_dialog("This one's also empty.","");
		if(get_flag(32,0) == 3)
			message_dialog("Another unoccupied room.  Dervish Malachai is good at this.","");
		if(get_flag(32,0) == 4)
			message_dialog("Well, this one's also unused.  That's all of them- and now to check on Malachai.","");
		inc_flag(32,0,1);
		set_flag(27,0,1);
	}

break;

beginstate 18;

	if(get_flag(28,0) == 0){
		if(get_flag(32,0) == 0)
			message_dialog("Nobody here.","");
		if(get_flag(32,0) == 1)
			message_dialog("Nobody here either.","");
		if(get_flag(32,0) == 2)
			message_dialog("This one's also empty.","");
		if(get_flag(32,0) == 3)
			message_dialog("Another unoccupied room.  Dervish Malachai is good at this.","");
		if(get_flag(32,0) == 4)
			message_dialog("Well, this one's also unused.  That's all of them- and now to check on Malachai.","");
		inc_flag(32,0,1);
		set_flag(28,0,1);
	}

break;

beginstate 19;

	if(get_flag(29,0) == 0){
		if(get_flag(32,0) == 0)
			message_dialog("Nobody here.","");
		if(get_flag(32,0) == 1)
			message_dialog("Nobody here either.","");
		if(get_flag(32,0) == 2)
			message_dialog("This one's also empty.","");
		if(get_flag(32,0) == 3)
			message_dialog("Another unoccupied room.  Dervish Malachai is good at this.","");
		if(get_flag(32,0) == 4)
			message_dialog("Well, this one's also unused.  That's all of them- and now to check on Malachai.","");
		inc_flag(32,0,1);
		set_flag(29,0,1);
	}

break;

beginstate 20;

	if(get_flag(30,0) == 0){
		if(get_flag(32,0) == 0)
			message_dialog("Nobody here.","");
		if(get_flag(32,0) == 1)
			message_dialog("Nobody here either.","");
		if(get_flag(32,0) == 2)
			message_dialog("This one's also empty.","");
		if(get_flag(32,0) == 3)
			message_dialog("Another unoccupied room.  Dervish Malachai is good at this.","");
		if(get_flag(32,0) == 4)
			message_dialog("Well, this one's also unused.  That's all of them- and now to check on Malachai.","");
		inc_flag(32,0,1);
		set_flag(30,0,1);
	}

break;

beginstate 21;

	if(get_flag(26,0) >= 15){
		message_dialog("Malachai is injured- you don't have the time to dawdle!","");
		block_entry(1);
		end();
	}

	if(get_flag(99,0) == 1){
		if(get_flag(32,0) >= 5)
			message_dialog("This room may be interesting, but it's not occupied by anyone.","Unfortunately, Malachai might need you.  You should probably attend to him.");
		if(get_flag(32,0) == 4)
			message_dialog("This room may be interesting, but it's not occupied by anyone.","You should check the room you've missed in the meantime.");
		if(get_flag(32,0) < 4)
			message_dialog("This room may be interesting, but it's not occupied by anyone.","You should check the rooms you've missed in the meantime.");
		block_entry(1);
		end();
	}

break;

beginstate 22;

	if(get_flag(31,0) == 0){
		inc_flag(32,0,1);
		if(get_flag(99,0) == 1){
			message_dialog("What is going on here?  The motif is decidedly bizarre for a Nephil's room.  There is a statue of what appears to be an Imperial noble, surrounded by Imperial emblems.","The whole setup is bizarre.  Still, you have your duty that must be completed first.");
			set_flag(31,0,2);
		}
		if(get_flag(99,0) == 2){
			message_dialog("What is going on here?  The motif is decidedly bizarre for a Nephil's room.  There is a statue of what appears to be an Imperial noble, surrounded by Imperial emblems.","There is bound to be something interesting, rewarding, or tactically valuable.  Orders of the Dervish be damned- you have your priority, and that is searching this room.");
			set_flag(31,0,1);
		}
	}

break;

beginstate 23;

	if(get_flag(31,0) < 2){
		reset_dialog();
		add_dialog_str(0,"This appears to be a journal written in the Imperial Common language- again, very bizarre for the typically savage nephils.  Dare you take a gander inside?",0);
		add_dialog_choice(0,"Read the book.");
		bmessage = run_dialog(0);

		reset_dialog();
		add_dialog_str(0,"It is a thin volume.  You start from the beginning...",30);
		add_dialog_str(1,"Dear self,",0);
		add_dialog_str(2,"It has been too long since I have felt the wind against my bare skin.  It has been too long since I have known what it feels like to be free.  Is this the price to pay for staying with my lord?",0);
		add_dialog_str(3,"This flesh is so real and natural that I have a difficult time imagining anything before it.  Eubans was attacked only one month ago, and Maxwell, the coward, fled.  Then, the black tendrils enveloped my body and mind.",0);
		add_dialog_str(4,"The change was slow but profound.  Eubans gave me a command to travel to the northern Ivalice Province.  Each excrutiating evening of travel caused my body to corrupt even further.  Maxwell remained pristine, damn him to hell.",0);
		if(get_flag(33,0) == 1){
			if(get_flag(8,0) == 1){
				if(get_flag(99,0) == 1)
					add_dialog_str(5,"Mawell, the soldier currently at Fort Ivalice?  This news would devastate him with guilt!  Did this person really know the same Maxwell that you do?  It's a small world after all.",0);
				if(get_flag(99,0) == 2)
					add_dialog_str(5,"Mawell, the psychotic soldier currently at Fort Ivalice?  This news would devastate him with guilt!  So THIS was the failure he was talking about...  This would probably drive him insane with guilt.",0);
			}
			if(get_flag(8,0) == 0){
				add_dialog_str(5,"Maxwell...  He's one of the soldiers at Fort Ivalice!  You had no idea that he was swallowing this much guilt- if he knew this, though, he might just die of it.",0);
			}
		}
		add_dialog_choice(0,"Read onward.");
		bmessage = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"I used to be a human being named Casimir.  I used to be a man who guarded Duke Eubans, who used to be a man.  Now, I am a vile and grotesquely large nephilim.",0);
		add_dialog_str(1,"I have power that is not mine, and a mind that is not under my control.  I knew this when I went to Northern Fortress Ivalice.  The NFI skeleton guard saw me, and they began attacking.  I tried to push back my rage, but...",0);
		add_dialog_str(2,"I lost control of myself.  I fell into a sleep-like euphoria, as my muscles moved themselves.  I felt no pain or pleasure, and watched absentmindedly as my newly-grown claws sunk themselves into the flesh of innocent soldiers.",0);
		add_dialog_str(3,"When I had left my trip, I found nephilim congratulating me.  There was a tigerish humanoid staring over at me.  He told the crowd that I was Zenbu, their new champion of the Ratbane Revival.  And so it went.",0);
		add_dialog_str(4,"With this body, I have to follow Sabertooth.  I have not seen my lord for so long, but I suspect that he too has been corrupted as I have.  I hate this life.  I hate Maxwell, Sabertooth, and everything that moves, including myself.",0);
		add_dialog_str(5,"You decide to take this book.  It has sentimental value and then some.",40);
		bmessage = run_dialog(1);

		set_terrain(10,31,218);
		set_flag(31,0,2);
		change_spec_item(2,1);
	}

break;

beginstate 24;

	if(get_flag(31,0) == 1){
		message_dialog("You can't leave yet, at least without fulfilling your curiosity.","Malachai is a solid fighter.  He can take care of himself for now.");
		block_entry(1);
	}

break;

beginstate 25;

	if(get_flag(32,0) < 5){
		message_dialog("This is clearly the room Malachai is fighting in.  It sounds like the fight is raging on madly, but Malachai is holding out.","You should fulfill your orders quickly so that you can join him.");
		block_entry(1);
		end();
	}

	if(get_flag(32,0) == 5 && get_flag(26,0) < 15){
		if(is_combat() == 1){
			block_entry(1);
			end();
		}

		relocate_character(0,14,20);
		activate_hidden_group(2);
		set_character_pose(8,1);
		force_view_center(14,20);
		set_character_facing(0,2);
		put_effect_on_char(0,12,1,2);
		change_char_health(0,-30);
		force_instant_terrain_redraw();
		run_animation_sound(69);
		pause(5);

		message_dialog("An enemy leaps out of the room just as you are about to enter.  You're only barely able to prevent lethal damage from the surprise attack!","");

		reset_dialog();
		if(get_flag(99,0) == 2)
			add_dialog_str(0,"This nephil is vile and grotesquely large...  This is the nephil you read about; it is the champion of their army.  This is the craven beast formerly known as Casimir.  You are certain of it.",0);
		if(get_flag(99,0) == 1)
			add_dialog_str(0,"Who is this thing?!  It is ginormous and plated in thick armor that apparently does not hinder it in the slightest.  A dangerous beast indeed!",0);
		add_dialog_str(1,"But if this beast is alive, then what about Dervish Malachai?  It has bloodied claws!  Was Malachai defeated, or did this beast simply get away?  Nevermind that, what will you do now?",0);
		add_dialog_str(2,"Suddenly, it speaks in a deep, forceful, raspy voice.",0);
		add_dialog_str(3,"_I am Zenbu.  Prepare to die, you--_  A pregnant pause occurs, as the beast looks over you.  _Of all the bloody luck...  Damnation and hellfire!_",0);
		if(get_flag(99,0) == 2){
			add_dialog_str(4,"What is troubling Zenbu like this?  You never met him as Zenbu or Casimir, so what is stopping him from striking you?",0);
			add_dialog_choice(0,"Hey, Casimir...");
		}
		if(get_flag(99,0) == 1){
			add_dialog_str(4,"It can talk in Imperial Common?  Who is this Zenbu, and why isn't it attacking you?",0);
			add_dialog_choice(0,"Who are you?!");
		}

		bmessage = run_dialog(1);

		if(get_flag(99,0) == 2){
			clear_buffer();
			append_string("He seems surprised, but not as much as you'd expect.  _Greetings to you too, ");
			append_char_name(0);
			append_string(".  So we finally meet._");
			get_buffer_text(custom_m2);

			reset_dialog();
			add_dialog_str(0,custom_m2,0);
			add_dialog_str(1,"He knows your name?!  But how is that possible?  How are you any different from any of the other reinforcements?",0);
			add_dialog_str(2,"_We both suffer.  I have orders not to kill you, but that is not all.  I do not want to kill you.  I know of you and your ways...  Let us take revenge on the world someday in the future._",0);
			add_dialog_choice(0,"Revenge on the world?");
			bmessage = run_dialog(1);

			reset_dialog();
			add_dialog_str(0,"_Yes...  I will avenge everything I hold dear.  You may avenge Chika.  Farewell._",0);
			add_dialog_str(1,"Chika?  No!",0);
			add_dialog_choice(0,"Wait, Casimir!");
			bmessage = run_dialog(1);
		}
		if(get_flag(99,0) == 1){
			clear_buffer();
			append_string("_You do not know me, but I know you, ");
			append_char_name(0);
			append_string(".  So we finally meet._");
			get_buffer_text(custom_m2);

			reset_dialog();
			add_dialog_str(0,custom_m2,0);
			add_dialog_str(1,"He knows your name?!  But how is that possible?  How are you any different from any of the other reinforcements from Kalzas?",0);
			add_dialog_str(2,"_We both suffer.  I have orders not to kill you, but that is not all.  I do not want to kill you.  You too may realize one day that you are alone.  Then, we may be friends at last._",0);
			add_dialog_choice(0,"Let's be friends regardless and end this.");
			bmessage = run_dialog(1);

			reset_dialog();
			add_dialog_str(0,"_You do not know this yet, but there are things that we two must do.  Our destinies are intertwined by a vile puppetmaker._  He pauses.",0);
			add_dialog_str(1,"_Let us be friends only after you learn the truth about Chika._",0);
			add_dialog_str(2,"Chika?  No!",0);
			add_dialog_choice(0,"Wait, Zenbu!");
			bmessage = run_dialog(1);
		}

		set_character_pose(8,2);
		rctx = 13;
		while(rctx <= 23){
			relocate_character(8,rctx,22);
			force_instant_terrain_redraw();
			pause(1);

			rctx = (rctx + 1);
		}
		erase_char(8);
		force_instant_terrain_redraw();
		pause(2);

		clear_buffer();
		append_string("_Nghh...  ");
		append_char_name(0);
		append_string("?  You're here...  I'm glad..._");
		get_buffer_text(custom_m);
		message_dialog(custom_m,"It's Dervish Malachai, and he sounds hurt!");

		rctx = 13;
		while(rctx >= 9){
			relocate_character(0,rctx,21);
			force_view_center(rctx,21);
			force_instant_terrain_redraw();
			pause(4);

			if(rctx == 11){
				pause(5);
				message_dialog("Malachai!  He's covered in blood, much of it likely his own.  A small army of nephils lines the floor, but his most likely attacker was Zenbu.","_You are alive?  You survived the commander...  That's good..._");
			}

			rctx = (rctx - 1);
		}

		reset_dialog();
		add_dialog_str(0,"You ask Dervish Malachai what happened as you look over his numerous wounds.  Honestly, none of them are fatal, even if they are very frequent.",0);
		add_dialog_str(1,"_I charged into this room, and it was mostly empty.  Their commander called in reinforcements, though, distracting me...  He snuck up without my knowing it..._",0);
		add_dialog_str(2,"Malachai coughs up some blood on the ground, getting more on his armor and some on you.",0);
		add_dialog_str(3,"_Ungh...  Sorry.  Their commander, though...  It is fierce and violent.  I couldn't bear it, but...  He fled, for some reason.  He would have won easily, though..._",0);
		add_dialog_choice(0,"Tell me more later.  Now, we flee.");
		bmessage = run_dialog(1);

		message_dialog("_Flee?  Yes, that's wise...  Come.  Let's go to the township of Ivalice.  It's not entirely constructed, but it has some beds in it..._","[Malachai has rejoined you.]");
		set_flag(26,0,15);
		add_char_to_party(9);
		block_entry(1);
	}

break;

